home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxbcache.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  4.4 KB  |  148 lines

  1. /* Copyright (C) 1995, 1996 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxbcache.c,v 1.2 2000/09/19 19:00:33 lpd Exp $ */
  20. /* Bitmap cache implementation */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gsmdebug.h"
  24. #include "gxbcache.h"
  25.  
  26. /* ------ Entire cache ------ */
  27.  
  28. /* Initialize a cache.  The caller must allocate and initialize */
  29. /* the first chunk. */
  30. void
  31. gx_bits_cache_init(gx_bits_cache * bc, gx_bits_cache_chunk * bck)
  32. {
  33.     bck->next = bck;
  34.     bc->chunks = bck;
  35.     bc->cnext = 0;
  36.     bc->bsize = 0;
  37.     bc->csize = 0;
  38. }
  39.  
  40. /* ------ Chunks ------ */
  41.  
  42. /* Initialize a chunk.  The caller must allocate it and its data. */
  43. void
  44. gx_bits_cache_chunk_init(gx_bits_cache_chunk * bck, byte * data, uint size)
  45. {
  46.     bck->next = 0;
  47.     bck->data = data;
  48.     bck->size = size;
  49.     bck->allocated = 0;
  50.     if (data != 0) {
  51.     gx_cached_bits_head *cbh = (gx_cached_bits_head *) data;
  52.  
  53.     cbh->size = size;
  54.     cb_head_set_free(cbh);
  55.     }
  56. }
  57.  
  58. /* ------ Individual entries ------ */
  59.  
  60. /* Attempt to allocate an entry.  If successful, set *pcbh and return 0. */
  61. /* If there isn't enough room, set *pcbh to an entry requiring freeing, */
  62. /* or to 0 if we are at the end of the chunk, and return -1. */
  63. int
  64. gx_bits_cache_alloc(gx_bits_cache * bc, ulong lsize, gx_cached_bits_head ** pcbh)
  65. {
  66. #define ssize ((uint)lsize)
  67.     ulong lsize1 = lsize + sizeof(gx_cached_bits_head);
  68.  
  69. #define ssize1 ((uint)lsize1)
  70.     uint cnext = bc->cnext;
  71.     gx_bits_cache_chunk *bck = bc->chunks;
  72.     uint left = bck->size - cnext;
  73.     gx_cached_bits_head *cbh;
  74.     gx_cached_bits_head *cbh_next;
  75.     uint fsize = 0;
  76.  
  77.     if (lsize1 > bck->size - cnext && lsize != left) {    /* Not enough room to allocate in this chunk. */
  78.     *pcbh = 0;
  79.     return -1;
  80.     }
  81.     /* Look for and/or free enough space. */
  82.     cbh = cbh_next = (gx_cached_bits_head *) (bck->data + cnext);
  83.     while (fsize < ssize1 && fsize != ssize) {
  84.     if (!cb_head_is_free(cbh_next)) {    /* Ask the caller to free the entry. */
  85.         if (fsize)
  86.         cbh->size = fsize;
  87.         *pcbh = cbh_next;
  88.         return -1;
  89.     }
  90.     fsize += cbh_next->size;
  91.     if_debug2('K', "[K]merging free bits 0x%lx(%u)\n",
  92.           (ulong) cbh_next, cbh_next->size);
  93.     cbh_next = (gx_cached_bits_head *) ((byte *) cbh + fsize);
  94.     }
  95.     if (fsize > ssize) {    /* fsize >= ssize1 */
  96.     cbh_next = (gx_cached_bits_head *) ((byte *) cbh + ssize);
  97.     cbh_next->size = fsize - ssize;
  98.     cb_head_set_free(cbh_next);
  99.     if_debug2('K', "[K]shortening bits 0x%lx by %u (initial)\n",
  100.           (ulong) cbh, fsize - ssize);
  101.     }
  102.     gs_alloc_fill(cbh, gs_alloc_fill_block, ssize);
  103.     cbh->size = ssize;
  104.     bc->bsize += ssize;
  105.     bc->csize++;
  106.     bc->cnext += ssize;
  107.     bck->allocated += ssize;
  108.     *pcbh = cbh;
  109.     return 0;
  110. #undef ssize
  111. #undef ssize1
  112. }
  113.  
  114. /* Shorten an entry by a given amount. */
  115. void
  116. gx_bits_cache_shorten(gx_bits_cache * bc, gx_cached_bits_head * cbh,
  117.               uint diff, gx_bits_cache_chunk * bck)
  118. {
  119.     gx_cached_bits_head *next;
  120.  
  121.     if ((byte *) cbh + cbh->size == bck->data + bc->cnext &&
  122.     bck == bc->chunks
  123.     )
  124.     bc->cnext -= diff;
  125.     bc->bsize -= diff;
  126.     bck->allocated -= diff;
  127.     cbh->size -= diff;
  128.     next = (gx_cached_bits_head *) ((byte *) cbh + cbh->size);
  129.     cb_head_set_free(next);
  130.     next->size = diff;
  131. }
  132.  
  133. /* Free an entry.  The caller is responsible for removing the entry */
  134. /* from any other structures (like a hash table). */
  135. void
  136. gx_bits_cache_free(gx_bits_cache * bc, gx_cached_bits_head * cbh,
  137.            gx_bits_cache_chunk * bck)
  138. {
  139.     uint size = cbh->size;
  140.  
  141.     bc->csize--;
  142.     bc->bsize -= size;
  143.     bck->allocated -= size;
  144.     gs_alloc_fill(cbh, gs_alloc_fill_deleted, size);
  145.     cbh->size = size;        /* gs_alloc_fill may have overwritten */
  146.     cb_head_set_free(cbh);
  147. }
  148.